﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CuaHangBanGiay._Library;
using CuaHangBanGiay.Models;

namespace CuaHangBanGiay.Controllers
{
    public class KichThuocController : Controller
    {
        //
        // GET: /KichThuoc/
        private CuaHangBanGiayDBContext db;
        public KichThuocController(CuaHangBanGiayDBContext db)
        {
            this.db = db;
        }
        public KichThuocController()
        {
            this.db=new CuaHangBanGiayDBContext();
        }
        public KichThuoc get_by_id(int id)
        {
            return db.ds_kichthuoc.FirstOrDefault(x => x.id == id);
        }
        public Boolean is_exist(int id)
        {
          KichThuoc u = (from kt in db.ds_kichthuoc
                             where kt.id == id
                             select kt).FirstOrDefault();
            return u == null ? false : true;
        }
        public int add(KichThuoc dt)
        {
            db.ds_kichthuoc.Add(dt);
            db.SaveChanges();
            return db.ds_kichthuoc.Max(x => x.id);
        }
        public Boolean delete(int id)
        {
            KichThuoc kq = db.ds_kichthuoc.Where(x => x.id == id).FirstOrDefault();
            if (kq == null) return false;
            db.ds_kichthuoc.Remove(kq);
            db.SaveChanges();
            return true;
        }
        public Boolean edit(KichThuoc kt)
        {
            if (this.is_exist(kt.id))
            {
                KichThuoc obj = this.get_by_id(kt.id);
                obj.giatri = kt.giatri;
                db.SaveChanges();
                return true;
            }
            return false;
        }
        public List<KichThuoc> timkiem(String id="", String tenkt="",String active="",Boolean order_des=true,int start=0,int count=-1)
        {
            List<KichThuoc> dskq = db.ds_kichthuoc.ToList();
            if (!id.Equals(""))
            {
                int id_tk = TextLibrary.ToInt(id);
                dskq = this.db.ds_kichthuoc.Where(x => x.id==id_tk).ToList();
                if (dskq == null) return new List<KichThuoc>();
            }
            dskq = dskq.Where(x => x.giatri.Contains(tenkt)).ToList();
            if (dskq == null) dskq=new List<KichThuoc>();
            if (!active.Equals(""))
            {
                Boolean active_tk = active.Equals("1") ? true : false;
                dskq = dskq.Where(x => x.active == active_tk).ToList();
                if (dskq == null) dskq = new List<KichThuoc>();
            }
            
            if (order_des)
                dskq = dskq.OrderByDescending(x => x.id).ToList();
            else
                dskq = dskq.OrderBy(x => x.id).ToList();
            if (dskq == null) dskq = new List<KichThuoc>();
            if (count >= 0)
            {
                dskq = dskq.Skip(start).Take(count).ToList();
                if (dskq == null) dskq = new List<KichThuoc>();
            }
            return dskq;
        }
        


    }
}
